package xuegao.study.mall.rbac.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.api.CommonPage;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.model.ums.UmsMenu;
import xuegao.study.mall.common.vo.ums.UmsMenuVo;
import xuegao.study.mall.rbac.service.UmsMenuService;

import java.util.List;

@Api(tags = "UmsMenuController")
@Tag(name = "UmsMenuController", description = "菜单管理相关接口")
@RestController
@RequestMapping("/menu")
public class UmsMenuController {

    @Autowired
    private UmsMenuService umsMenuService;

    @ApiOperation("创建菜单")
    @PostMapping("/create")
    public CommonResult<Integer> addMenu(@RequestBody UmsMenu menu) {
        int count = umsMenuService.addMenu(menu);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Integer> deleteMenu(@PathVariable("id") Long id) {
        int count = umsMenuService.deleteMenuById(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("更新菜单")
    @PostMapping("/update/{id}")
    public CommonResult<Integer> updateMenu(@PathVariable("id") Long id, @RequestBody UmsMenu menu) {
        int count = umsMenuService.updateMenu(id, menu);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("查询菜单")
    @GetMapping("/list/{parentId}")
    public CommonResult<CommonPage<UmsMenu>> listMenu(
            @PathVariable("parentId") Long parentId,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        List<UmsMenu> list = umsMenuService.selectList(parentId, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("树形菜单")
    @GetMapping("/treeList")
    public CommonResult<List<UmsMenuVo>> treeList() {
        List<UmsMenuVo> umsMenuVos = umsMenuService.treeList();
        return CommonResult.success(umsMenuVos);
    }

    @ApiOperation("根据id获取菜单详情")
    @GetMapping("/{id}")
    public CommonResult<UmsMenu> getMenu(@PathVariable("id") Long id) {
        UmsMenu umsMenu = umsMenuService.getUmsMenuById(id);
        return CommonResult.success(umsMenu);
    }
}
